package com.zbec.mdrs.repository.jpa;

import com.zbec.mdrs.entity.discount.Discount;
import com.zbec.mdrs.vo.DiscountVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by fxl on 2015/10/17.
 */
public interface DiscountDao extends PagingAndSortingRepository<Discount, Long>, JpaSpecificationExecutor<Discount> {

    List<Discount> findByHistoryId(Long historyId);

    List<Discount> findByHistoryIdAndOrgId(Long historyId,Long orgId);

    @Query("select new com.zbec.mdrs.vo.DiscountVo(d.id,d.zzjgdm,d.sjsj,d.jrjgbm,d.jrjgwdmc,d.wdssqu,d.txsqrmc," +
            "d.pjzl,d.cdhmc,d.bz,d.txje,d.zsrmb,d.zsmy,d.txrq,d.cdrq,d.txll,d.pmje,d.wjfl,d.cdhdq,d.pjnbbh," +
            "d.source,d.recordStatus,d.reportStatus,d.existError,d.errorDesc,d.missedField,d.recordId) from Discount d  " +
            "where (''= :sjsj or d.sjsj = :sjsj) "+
            "and (''= :recordStatus or d.recordStatus = :recordStatus) " +
            "and (''= :existError or d.existError = :existError)  ")
    Page<DiscountVo> findAllDiscount(@Param("sjsj")String sjsj,@Param("recordStatus")String recordStatus,@Param("existError")String existError,Pageable pageable);

  @Query("select new com.zbec.mdrs.vo.DiscountVo(d.id,d.zzjgdm,d.sjsj,d.jrjgbm,d.jrjgwdmc,d.wdssqu,d.txsqrmc," +
            "d.pjzl,d.cdhmc,d.bz,d.txje,d.zsrmb,d.zsmy,d.txrq,d.cdrq,d.txll,d.pmje,d.wjfl,d.cdhdq,d.pjnbbh," +
            "d.source,d.recordStatus,d.reportStatus,d.existError,d.errorDesc,d.missedField,d.recordId) from Discount d  " +
            "where (''= :historyId or d.history.id = :historyId) ")
    Page<DiscountVo> findDiscountByHistroy(@Param("historyId")String historyId,Pageable pageable);

    @Query("select l from Discount l  " +
            "where (''= :sjsj or l.sjsj = :sjsj) "+
            "and (''= :recordStatus or l.recordStatus = :recordStatus) " +
            "and (''= :existError or l.existError = :existError) order by l.zzjgdm  ")
    List<Discount>  findDiscountForExport(@Param("sjsj")String sjsj,@Param("recordStatus")String recordStatus,@Param("existError")String existError);

    @Query("select l from Discount l  " +
            "where l.sjsj = :sjsj  and l.recordStatus <>  :recordStatus  and  l.pjnbbh = :pjnbbh  ")
    List<Discount> findUpdateDiscount(@Param("pjnbbh")String pjnbbh ,@Param("sjsj")String sjsj,@Param("recordStatus")String recordStatus);


    @Query( "select max(sjsj) from Discount where sjsj < :sjsj")
    String getMaxDiscountFny(@Param("sjsj")String sjsj);


    @Query("select l from Discount l where l.recordStatus !=3 and l.recordId = :recordId and l.sjsj = :sjsj ")
    Discount getDiscountFromDrmap(@Param("recordId")Long recordId,@Param("sjsj")String sjsj);

    @Query("select l from Discount l  " +
            "where l.sjsj = :sjsj  and l.recordStatus = :recordStatus  and  l.reportStatus <> :reportStatus " +
            "and (''= :jrjgbm or l.jrjgbm = :jrjgbm) order by l.zzjgdm  ")
    List<Discount> getActiveDiscountByYwMonth(@Param("sjsj")String sjsj ,@Param("recordStatus")String recordStatus,
                                              @Param("reportStatus")String reportStatus,@Param("jrjgbm")String jrjgbm);

}
